<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="hSave">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson.js'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting.js'

export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    // 获取权限列表
    this.loadPermissionList()
  },
  methods: {
    //  获取权限列表
    async loadPermissionList() {
      const { data: res } = await getPermissionList()
      // console.log(res)
      this.list = tranListToTreeData(res)
      // 获取角色权限详情
      const { data } = await getRoleDetail(this.roleId)
      // console.log(data)
      this.$refs.refTree.setCheckedKeys(data.permIds)
    },
    // 点击确认
    async hSave() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds })
      this.$emit('close')
      this.$message.success('保存角色的权限成功')
    }
  }
}
</script>

<style></style>
